package cn.jdy.plugins.web;

import cn.jdy.anquan.entity.CompanySafe;
import cn.jdy.anquan.service.CompanySafeService;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.authority.service.SysRoleService;
import cn.jdy.authority.service.UserService;
import cn.jdy.plugins.entity.Company;
import cn.jdy.plugins.entity.Info;
import cn.jdy.plugins.entity.Member;
import cn.jdy.plugins.service.InfoService;
import cn.jdy.plugins.service.MemberService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息控制类
 */
@Controller
@RequestMapping("/info")
public class InfoCtrl extends BaseCtrl {
	@Autowired
	private InfoService infoService;
	@Autowired
	public SysRoleService sysRole;
	@Autowired
	private MemberService memberService;// 会员分类业务处理类
	@Autowired
	private UserService userManage;
	@Autowired
	private CompanySafeService companySafeService;


    /**
     * 初始化用户发送消息
     * @return
     */
    @RequestMapping("/initSendMsg")
    public ModelAndView initSendMsg(){
        ModelAndView mv = new ModelAndView("/my/myWriteInfo.jsp");
        /* 获取将要发送人列表*/
//
//        List<User> list = sysRole.findUsers("cdc67e472bfa4b7a905f616384695872");
//        mv.addObject("dataList", list);

        return  mv;
    }
	/**
	 * 我的消息信息
	 *
	 * @param bo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/forMyinfo")
	public ModelAndView forMyMessage(CompanySafe bo,Integer pageNo,Integer pageSize,String searchtype) {
		ModelAndView mv = new ModelAndView("/my/myMsgInfo.jsp");
		Member memberinfo = getMember();
		Integer companyid = memberinfo.getCompany().getId();
		bo.setStatus("1");
		Company company = new Company();
		company.setId(companyid);
		bo.setCompany(company);
 
		try {
			/************* 分页处理开始 ************/
		  if(pageNo==null) pageNo = 1;
	    if(pageSize==null)pageSize = 10;
	    bo.setiDisplayStart((pageNo - 1) * pageSize);//查询起始值
	    bo.setiDisplayLength(pageSize);//查询条数
	    
			String Count = companySafeService.getCount(bo);//查询我的消息信息总条数
			List<CompanySafe> list = companySafeService.query(bo);//获取page信息
			/************* 分页处理结束 ***********/

			mv.addObject("dataList", list);
			mv.addObject("pageNo", bo.getPageNo());
			mv.addObject("total", Count);
			mv.addObject("size", pageSize);
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询我的网站信息错误", e);
		}
		return mv;
	}


    /**
     * 根据id 查看消息信息
     * @param id
     * @return
     */
    @RequestMapping("/findInfoById")
    public ModelAndView findInfoById(String id,Integer currentPage,String searchtype){
        ModelAndView mv = new ModelAndView("/my/myInfoDetail.jsp");
        try{
            if(null!=id && !"".equals(id)){
                Info info = infoService.find(id);
                mv.addObject("infobo",info);
                mv.addObject("status",200);
                if(info!=null){
                    info.setFlag("1");
                    infoService.update(info);
                   // infoService.saveOrUpd(info);
                }

            }else{
                mv.addObject("status",500);
            }
            mv.addObject("currentPage", currentPage);
            mv.addObject("searchtype", searchtype);
        }catch (Exception e){
            mv.addObject("status",500);
            BaseLog.e(this.getClass(),"查找消息失败！",e);
        }

        return mv;
    }

    /**
	 * 保存消息
	 *
	 * @param bo
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Map<String, Object> save(Info bo) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
            bo.setType("4");
            bo.setSendUserId(getMember().getId());
			infoService.saveOrUpd(bo);
			jsonMap.put("status", 200);
		} catch(Exception e) {
			jsonMap.put("status", 500);
			BaseLog.e(this.getClass(), "保存消息异常", e);
		}
		return jsonMap;
	}

	/**
	 * 消息记录数
	 *
	 */
	@RequestMapping("/getInfoCount")
	@ResponseBody
	public Map<String, Object>  getInfoCount(){
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("infoCount", 0);
		try {
			Member memberinfo = getMember();
			if(memberinfo!=null){
				Info info = new Info();
				info.setUserid(memberinfo.getId());
				info.setFlag("0");
				String Count = infoService.getInfoCount(info);//查询我的消息信息总条数
				jsonMap.put("infoCount", Count);
			}
			jsonMap.put("status", 200);
		} catch(Exception e) {
			jsonMap.put("status", 500);
			BaseLog.e(this.getClass(), "保存消息异常", e);
		}
		return jsonMap;

	}

	/**
	 * 删除消息
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	public ModelAndView delete(String id){
		ModelAndView mv = new ModelAndView("redirect:/info/forMyinfo?searchtype=1");
		try {
			if(id!=null && id.length()>0)
				infoService.delete(id);
		}catch(Exception e) {
			BaseLog.e(this.getClass(), "删除消息失败：" + id, e);
		}
		return mv;
	}
}